/**
 * Created by HaiHui on 2017/10/17.
 */
var table;
$(document)
		.ready(
				function() {
					table = $('#example')
							.DataTable(
									{
										"pagingType" : "full_numbers",// 设置分页控件的模式
										searching : false,// 屏蔽datatales的查询框
										aLengthMenu : [ 3 ],// 设置一页展示10条记录
										bRetrieve : true,
										"bLengthChange" : false,// 屏蔽tables的一页展示多少条记录的下拉列表
										"ordering" : false,// 屏蔽排序
										"oLanguage" : { // 对表格国际化
											"sLengthMenu" : "每页显示 _MENU_条",
											"sZeroRecords" : "没有找到符合条件的数据",
											/*"sProcessing" : "&lt;img src=’./loading.gif’ /&gt;",*/
											"sInfo" : "当前第 _START_ - _END_ 条　共计 _TOTAL_ 条",
											"sInfoEmpty" : "木有记录",
											"sInfoFiltered" : "(从 _MAX_ 条记录中过滤)",
											"sSearch" : "搜索：",
											"oPaginate" : {
												"sFirst" : "首页",
												"sPrevious" : "前一页",
												"sNext" : "后一页",
												"sLast" : "尾页"

											}
										},
										"processing" : true, // 打开数据加载时的等待效果
										"serverSide" : true,// 打开后台分页

										"ajax" : {
											"url" : "sysuser/list",
											"dataSrc" : "aaData",
											"data" : function(d) {
												d.aduserNameSelect = $(
														'#aduserNameSelect')
														.val();
												d.aduserIdSelect= $(
												'#aduserIdSelect')
												.val();
												// 添加额外的参数传给服务器
											}
										},
										"columns" : [  {
											"data" : "aduserName"
										}, {
											"data" : "aduserTel"
										}, {
											"data" : "unionId"
										},{
											"data" : "roleName"
										},{
											"data" : "aduserDustName"
										},  {
											"data" : "aduserId" ,"visible":false
										},{
											"data" : null
										} ],
										"columnDefs" : [ {
											"targets" : -1,// 操作按钮目标列
											"data" : null,
											"defaultContent" : "<i class=\"dogo-js-search fa fa-search\">详细</i>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<i class=\"dogo-js-edit fa fa-edit\">修改</i>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<i class=\"dogo-js-del fa fa-trash-o\">删除</i>"

										} ]
									});
					// 查看
					$('#example tbody').on(
							'click',
							'.dogo-js-search',
							function() {
								var data = table.row($(this).parents('tr'))
										.data();
								showDetail(data.aduserId)
								
							});
					// 删除
					$('#example tbody').on('click', '.dogo-js-del', function() {
						var data = table.row($(this).parents('tr')).data();
						var confirmIdx = layer.confirm("确认要删除?", {
							btn : [ '删除', '取消' ]
						}, function() {
							var setting = {
								url : "sysuser/deleteUser",
								data : {
									id : data.aduserId
								}
							}
							ajaxSubmit(setting, function(responseData) {
								checkSubmitResult(responseData, {
									error : "删除失败"
								}, function(responseData) {
									layer.alert(responseData.description);
									$('#example').DataTable().ajax.reload();

								});
							}, "删除失败");
						}, function() {
							layer.close(confirmIdx);
						});

					});
					// edit
					$('#example tbody').on(
							'click',
							'.dogo-js-edit',
							function() {
                             
								var data = table.row($(this).parents('tr'))
										.data();
								showEditModal(data.aduserId)
							});

				});

function search() {
	table.ajax.reload();
}

//新增画面显示
function showAddModal() {
	debugger;
    $("#modal-label").text("添加人员信息");
    $("#saveProjectInfo").text("添加");
    $("#saveProjectInfo").data("opType", "add");
    $("#aduserId").val("");
    $("#aduserName").val("");
    $("#edit-modal").find("#aduserTel").val("");
    $("#edit-modal").find("#unionId").val("");
    $("#edit-modal").find("#userRoleId").val("");
    $('#edit-modal').modal();
    initCombo('unionId', 'area/townList', 'xingzhengBianhao', 'xingzhengMingcheng');
    initCombo('userRoleId', 'sysuser/sysUserRoleList', 'roleId', 'roleName');
    initCombo('aduserDustId', 'sysuser/sysUserDustList', 'dustId', 'dustName');
}

//修改画面显示
function showEditModal(dataId) {
    $("#modal-label").text("修改人员信息");
    $("#saveProjectInfo").text("修改");
    $("#saveProjectInfo").data("opType", "edit");
    var setting = {
        url: "sysuser/detail",
        data: {
            id: dataId
        }
    }
    ajaxSubmit(setting, function(responseData) {
        checkSubmitResult(responseData, {
            error: "获取项目详情失败"
        }, function(responseData) {
            //TODO 详情页面数据填充
            var data = responseData.data;
            $("#edit-modal").find("#aduserId").val(data.aduserId);
            $("#edit-modal").find("#aduserName").val(data.aduserName);
            $("#edit-modal").find("#aduserTel").val(data.aduserTel);
            initCombo('unionId', 'area/townList', 'xingzhengBianhao', 'xingzhengMingcheng',data.unionId);
            initCombo('userRoleId', 'sysuser/sysUserRoleList', 'roleId', 'roleName',data.userRoleId);
            initCombo('aduserDustId', 'sysuser/sysUserDustList', 'dustId', 'dustName',data.aduserDustId);
            $("#edit-modal").modal('show');
        });
    }, "获取项目详情失败");

}
//详细画面显示
function showDetail(dataId) {
    var setting = {
        url: "sysuser/detail",

        data: {
            id: dataId
        }
    }
    ajaxSubmit(setting, function(responseData) {
        checkSubmitResult(responseData, {
            error: "获取项目详情失败"
        }, function(responseData) {
            //TODO 详情页面数据填充
            var data = responseData.data;
            $("#search-modal").find("#infoAduserId").text(data.aduserId);
            $("#search-modal").find("#infoAduserName").text(data.aduserName);
            $("#search-modal").find("#infoAduserTel").text(data.aduserTel);
            $("#search-modal").find("#infoUnionId").text(data.areaName);
            $("#search-modal").find("#inforoleId").text(data.roleName);
            $("#search-modal").find("#infoDustId").text(data.aduserDustName);
            $("#search-modal").modal('show');
        });
    }, "获取项目详情失败");
}
//添加、修改项目
$("#saveProjectInfo").click(function() {
	debugger
    var opType = $(this).data("opType");
    var action = null;
    var formdata = {};
    formdata =  $("#editForm").serializeJson()
    var errorMsg = null;
    if(opType == "add") {
        action = "sysuser/add";
        errorMsg = "添加信息失败";
    } else if(opType == "edit") {
        action = "sysuser/updateUser";
        errorMsg = "修改信息失败";
    }
    if(action == null) {
        layer.alert("不支持的操作类型");
    } else {
        debugger;
        var setting = {
            url: action,
            data: JSON.stringify(formdata),
            cache: false,
            contentType:"application/json",
            processData: false,
            async: false

        }
        ajaxSubmit(setting, function(responseData) {
            checkSubmitResult(responseData, {
                error: errorMsg
            }, function(responseData) {
                $("#edit-modal").modal('hide');
                layer.alert(responseData.description);
                $('#example').DataTable().ajax.reload();

            });

        }, errorMsg);

    }});
//判断用户名是否存在
$(function(){
	
	   $("#aduserName").on('blur',function(){
		   var sfzh = $("#aduserName").val();
		   if (sfzh == "") {
	           return;
	       }
		   else
		   {
			   var setting = {
				        url: "sysuser/getUserByName",
				        data: {
				        	userName:sfzh
				        },
				        dataType:'json'

				    }
				    ajaxSubmit(setting, function(responseData) {
				        checkSubmitResult(responseData, {
				            error: "获取数据失败"
				        }, function(responseData) {
				           // $("#addRegionModel").modal('hide');
				        	var data = responseData.data;
				        	if(data>0)
				            {
				        		layer.alert(responseData.description);
				        	}else
				        	{
				        		return;
				        		
				        	}
				            


				        });

				    }, "获取数据失败");
			   
			   
		   }
	   });
 })
